package com.lyw.home

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.lyw.db.QuestionType
import com.lyw.home.bean.BaseQuestion
import com.lyw.home.bean.QuestionBean
import com.lyw.home.bean.QuestionShortBean
import com.lyw.home.databinding.ItemQuestionBinding
import com.lyw.home.databinding.ItemShortQuestionBinding
import io.noties.markwon.Markwon
import me.shouheng.utils.ktx.onDebouncedClick

class QuestionAdapter(
    private var questions: List<BaseQuestion>,
    private val listener: OnAnswerSelectedListener
) : BaseMultiItemAdapter<BaseQuestion>() {
    class QuestionViewHolder(val binding: ItemQuestionBinding) : RecyclerView.ViewHolder(binding.root)
    class QuestionShortViewHolder(val binding: ItemShortQuestionBinding) : RecyclerView.ViewHolder(binding.root)
    init {
        addItemType(QuestionType.Selection.ordinal,object:OnMultiItemAdapterListener<BaseQuestion,QuestionViewHolder>{
            override fun onBind(holder: QuestionViewHolder, position: Int, item: BaseQuestion?) {
                LogUtils.d(item)
                item?.let {
                    onBindSelectionQuestionViewHolder(holder, position,item as QuestionBean);
                }

            }
            override fun onCreate(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): QuestionViewHolder {
                LogUtils.d("onCreate viewType: $viewType")
               return QuestionViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context),R.layout.item_question,parent,false));
            }
        })
        addItemType(QuestionType.ShortQuestion.ordinal,object:OnMultiItemAdapterListener<BaseQuestion,QuestionShortViewHolder>{
            override fun onBind(holder: QuestionShortViewHolder, position: Int, item: BaseQuestion?) {
                LogUtils.d(item)
                item?.let {
                    onBindShortQuestionViewHolder(holder, position,item as QuestionShortBean);
                }
            }
            override fun onCreate(
                context: Context,
                parent: ViewGroup,
                viewType: Int
            ): QuestionShortViewHolder {
                LogUtils.d("onCreate viewType: $viewType")
                return QuestionShortViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context),
                    R.layout.item_short_question,parent,false));
            }
        })
    }

    override fun getItemViewType(position: Int, list: List<BaseQuestion>): Int {
        return list[position].type
    }

    interface OnAnswerSelectedListener {
        fun onAnswerSelected(question: QuestionBean, selectedOptionIndex: Int)
    }

    fun onBindShortQuestionViewHolder(holder: QuestionShortViewHolder, position: Int,question: QuestionShortBean) {
//        holder.binding.question = question
        holder.binding.executePendingBindings()
        holder.binding.questionText.text = question.question
        Markwon.create(context).setMarkdown(holder.binding.answer,question.answer)
//        holder.binding.answer.text = question.answer
        holder.binding.answer.isVisible = question.showAnswer
        holder.binding.questionText.onDebouncedClick {
            question.showAnswer = !question.showAnswer
            notifyItemChanged(position)
        }
    }

     fun onBindSelectionQuestionViewHolder(holder: QuestionViewHolder, position: Int,question: QuestionBean) {
//        val question = questions[position]
        holder.binding.question = question
        holder.binding.executePendingBindings()
        holder.binding.optionA.text = question.options.getOrNull(0)
        holder.binding.optionB.text = question.options.getOrNull(1)
        holder.binding.optionC.text = question.options.getOrNull(2)
        holder.binding.optionD.text = question.options.getOrNull(3)

        holder.binding.optionA.onDebouncedClick {
            listener.onAnswerSelected(question, 0)
        }
        holder.binding.optionB.onDebouncedClick {
            listener.onAnswerSelected(question, 1)
        }
        holder.binding.optionC.onDebouncedClick {
            listener.onAnswerSelected(question, 2)
        }
        holder.binding.optionD.onDebouncedClick {
            listener.onAnswerSelected(question, 3)
        }
        var optionsView = arrayOf<View>(holder.binding.optionA,holder.binding.optionB,holder.binding.optionC,holder.binding.optionD)
        question.option?.let {
            changeButtonBackground(question,optionsView)
        }?:let {
            optionsView.forEach {
                it.setBackgroundResource(R.drawable.option_background)
            }
        }
    }
    private fun changeButtonBackground(question: QuestionBean, optionsView: Array<View>) {
           optionsView.forEach {
               if(question.option==optionsView.indexOf(it)){
//                   if(question.isCorrect) {
//                       it.setBackgroundResource(R.drawable.option_background_correct)
//                   }else{
                       it.setBackgroundResource(R.drawable.option_background_selected)
//                   }
               }else{
                   it.setBackgroundResource(R.drawable.option_background)
               }
           }
//        }
    }

    fun updateData(newQuestions: List<BaseQuestion>) {
        questions = newQuestions
        notifyDataSetChanged()
    }


}